//
//  WCBuildDefine.m
//  WabbitCode
//
//  Created by William Towe on 12/13/08.
//  Copyright 2008 Revolution Software. All rights reserved.
//

#import "WCBuildDefine.h"
#import "WCMacros.h"


#define kWCBDNameKey @"name"
#define kWCBDValueKey @"value"

static NSCharacterSet *allowedChars = nil;

@implementation WCBuildDefine
#pragma mark *** Subclass Overrides ***
- (id)init {
	return [self initWithName:NSLocalizedString(@"NewDefine",@"NewDefine")];
}

- (void)dealloc {
	[_name release];
	[_value release];
	[super dealloc];
}
#pragma mark NSCoding Protocol
- (id)initWithCoder:(NSCoder *)coder {
	if (!(self = [super init]))
		return nil;
	
	_name = [[coder decodeObjectForKey:kWCBDNameKey] retain];
	_value = [[coder decodeObjectForKey:kWCBDValueKey] retain];
	
	return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
	[coder encodeObject:_name forKey:kWCBDNameKey];
	[coder encodeObject:_value forKey:kWCBDValueKey];
}
#pragma mark NSCopying Protocol
- (id)copyWithZone:(NSZone *)zone {
	WCBuildDefine *copy = [[[self class] alloc] init];
	
	copy->_name = [_name copy];
	copy->_value = [_value copy];
	
	return copy;
}
#pragma mark NSMutableCopying Protocol
- (id)mutableCopyWithZone:(NSZone *)zone {
	WCBuildDefine *copy = [[[self class] alloc] init];
	
	copy->_name = [_name copy];
	copy->_value = [_value copy];
	
	return copy;
}

#pragma mark Validation
- (BOOL)validateName:(id *)value error:(NSError **)error {
	if (*value == nil || ![*value length]) {
		*value = _name;
		return YES;
	}
	
	if (!allowedChars)
		allowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"] retain];
	
	NSUInteger index, length = [*value length];
	NSMutableString *newString = [NSMutableString string];
	
	for (index=0; index<length; index++) {
		if ([allowedChars characterIsMember:[*value characterAtIndex:index]])
			[newString appendFormat:@"%C", [*value characterAtIndex:index]];
	}
	
	if ([newString length])
		*value = [[newString copy] autorelease];
	else
		*value = _name;
	
	return YES;
}

- (BOOL)validateValue:(id *)value error:(NSError **)error {
	if (!allowedChars)
		allowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"] retain];
	
	NSUInteger index, length = [*value length];
	NSMutableString *newString = [NSMutableString string];
	
	for (index=0; index<length; index++) {
		if ([allowedChars characterIsMember:[*value characterAtIndex:index]])
			[newString appendFormat:@"%C", [*value characterAtIndex:index]];
	}
	
	*value = [[newString copy] autorelease];
	return YES;
}
#pragma mark *** Public Methods ***
#pragma mark Accessors
@synthesize value=_value,name=_name;

@dynamic nameAndValue;
- (NSString *)nameAndValue {
	if ([self value])
		return [NSString stringWithFormat:@"-D%@=%@", [self name], [self value]];
	return [NSString stringWithFormat:@"-D%@", [self name]];
}


#pragma mark Creation
+ (id)buildDefineWithName:(NSString *)name; {
	return [[[[self class] alloc] initWithName:name] autorelease];
}

+ (id)buildDefineWithName:(NSString *)name value:(NSString *)value; {
	return [[[[self class] alloc] initWithName:name value:value] autorelease];
}

- (id)initWithName:(NSString *)name; {
	return [self initWithName:name value:nil];
}

- (id)initWithName:(NSString *)name value:(NSString *)value; {
	if (!(self = [super init]))
		return nil;
	
	_name = [name copy];
	_value = [value copy];
	
	return self;
}

@end
